/**
  ******************************************************************************
  * @file    EEPROM_Driver.h
  * @author  tuancheng_Zero
  * @version V0.1
  * @date    08/05/2024
  * @brief   extern Sram Drive
  ******************************************************************************
*/ 
#ifndef  __EEPROM_DRIVER_H__
#define  __EEPROM_DRIVER_H__
#ifdef __cplusplus
extern "C" {
#endif

/**
  * @brief GLOBALS RAM definition
  */
#ifdef   EEPROM_GLOBALS
#define  EEPROM_DRIVER_EXT
#else
#define  EEPROM_DRIVER_EXT  extern
#endif

//全局宏定义
#define EEPROM_DMA_WR_BUFF_SIZE      260   // 发送dma缓冲区大小(CAT25256的页大小为64字节)
#define EEPROM_DMA_RD_BUFF_SIZE      260   // 接收dma缓冲区大小

#define EEPROMCMD_WREN               0x06  // 写使能
#define EEPROMCMD_WRITE              0x02  // 写数据
#define EEPROMCMD_RDSR               0x05  // 读状态寄存器
#define EEPROMCMD_READ               0x03  // 读数据

//gpio配置
#define EEPROM_GPIOx                 GPIOE
#define EEPROM_GPIO_RCC_Periph       RCC_APB2_PERIPH_GPIOE    
#define EEPROM_GPIO_Mode_MOSI        GPIO_Mode_AF_PP          
#define EEPROM_GPIO_Pin_MOSI         GPIO_PIN_13
#define EEPROM_GPIO_Mode_MISO        GPIO_Mode_IPU     
#define EEPROM_GPIO_Pin_MISO         GPIO_PIN_12
#define EEPROM_GPIO_Mode_SCK         GPIO_Mode_AF_PP           
#define EEPROM_GPIO_Pin_SCK          GPIO_PIN_11
#define EEPROM_GPIO_Mode_CS          GPIO_Mode_Out_PP   
#define EEPROM_GPIO_Pin_CS           GPIO_PIN_14

//spi配置
#define EEPROM_SPI                   SPI2
#define EEPROM_Direction             SPI_DIR_DOUBLELINE_FULLDUPLEX
#define EEPROM_Mode                  SPI_MODE_MASTER
#define EEPROM_DataSize              SPI_DATA_SIZE_8BITS
#define EEPROM_CPOL                  SPI_CLKPOL_HIGH
#define EEPROM_CPHA                  SPI_CLKPHA_SECOND_EDGE
#define EEPROM_NSS                   SPI_NSS_SOFT
#define EEPROM_BaudRatePres          SPI_BR_PRESCALER_32
#define EEPROM_FirstBit              SPI_FB_MSB
#define EEPROM_CRCPoly               7

//DMA配置
#define EEPROM_DMA_RCC_Periph        RCC_AHBPeriph_DMA1 
#define EEPROM_DR_Address            ((uint32_t)&SPI2->DAT)
#define EEPROM_DMA_Rx_IRQn           DMA1_Channel4_IRQn
#define EEPROM_DMA_Tx_Channel        DMA1_CH5
#define EEPROM_DMA_Rx_Channel        DMA1_CH4
#define EEPROM_DMA_Rx_FLAG           (DMA1_FLAG_GL4|DMA1_FLAG_TC4|DMA1_FLAG_TE4|DMA1_FLAG_HT4)  
#define EEPROM_DMA_Rx_Function       DMA1_Channel4_IRQHandler

EEPROM_DRIVER_EXT void EEPROM_Init(void);
EEPROM_DRIVER_EXT uint8_t EEPROM_ReadData(uint16_t addr, uint8_t * readData, uint16_t length);
EEPROM_DRIVER_EXT uint8_t EEPROM_WriteData(uint16_t addr, uint8_t * writeData, uint16_t length);

#ifdef __cplusplus
}
#endif
#endif   /*   __EXTEND_EEPROM_H__   */
